package priv.happy.mealpickpro.scheduling;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import priv.happy.mealpickpro.domain.Ranking;
import priv.happy.mealpickpro.domain.UserMeal;
import priv.happy.mealpickpro.dto.UserFoodCount;
import priv.happy.mealpickpro.dto.UserPickCount;
import priv.happy.mealpickpro.dto.UserTagCount;
import priv.happy.mealpickpro.service.*;

/**
 * @Author: HAPPY
 * @Project_name: mealPickPro
 * @Package_name: priv.happy.mealpickpro.count
 * @Date: 2023/10/21 18:55
 * @Description: 每一个小时统计一次各个账号的标签、食物、选餐信息，更新到排行榜数据库中
 */
@Component
public class RankingCount {

    @Autowired
    TagService tagService;

    @Autowired
    FoodService foodService;

    @Autowired
    RankingService rankingService;

    @Autowired
    UserMealService userMealService;

    @Autowired
    PickLogService pickLogService;

    /**
     * 每小时统计一次标签信息排行榜，更新当天的num，延迟1分钟执行，与每日整点跟新错开
     */
    @Scheduled(cron = "30 0 */1 * * ?")
    public void statisticalHoursTagRankingInformation() {
        System.out.println(DateUtil.now() + "执行每小时统计标签信息任务开始==========================");
        for (UserTagCount countUserTag : tagService.countUserTags()) {
            rankingService.update(new UpdateWrapper<Ranking>()
                    .set("num", countUserTag.getCount())
                    .likeRight("date", DateUtil.now().substring(0, 10))
                    .eq("user_id", countUserTag.getUserId())
                    .eq("type", "标签"));
        }
        System.out.println(DateUtil.now() + "执行每小时统计标签信息任务结束==========================");
    }

    /**
     * 每天00:00:00统计一次标签信息排行榜，新增一条记录
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void statisticalDailyTagRankingInformation() {
        System.out.println(DateUtil.now() + "执行每日0时统计标签信息任务开始==========================");
        for (UserTagCount countUserTag : tagService.countUserTags()) {
            UserMeal userMeal = userMealService.getById(countUserTag.getUserId());
            rankingService.save(new Ranking().setDate(DateUtil.now())
                    .setNum(countUserTag.getCount()).setUserId(countUserTag.getUserId())
                    .setType("标签")
                    .setUserName(userMeal.getName())
                    .setUserAvatar(userMeal.getAvatar()));
        }
        System.out.println(DateUtil.now() + "执行每日0时统计标签信息任务结束==========================");
    }

    /**
     * 每小时统计一次食物信息排行榜，更新当天的num，第二分钟开始
     */
    @Scheduled(cron = "30 1 */1 * * ?")
    public void statisticalHoursFoodRankingInformation() {
        System.out.println(DateUtil.now() + "执行每小时统计食物信息任务开始==========================");
        for (UserFoodCount countUserTag : foodService.countUserFoods()) {
            rankingService.update(new UpdateWrapper<Ranking>()
                    .set("num", countUserTag.getCount())
                    .likeRight("date", DateUtil.now().substring(0, 10))
                    .eq("user_id", countUserTag.getUserId())
                    .eq("type", "食物"));
        }
        System.out.println(DateUtil.now() + "执行每小时统计食物信息任务结束==========================");
    }

    /**
     * 每天统计00:01:00一次食物信息排行榜，新增一条记录,30秒开始
     */
    @Scheduled(cron = "0 1 0 * * ?")
    public void statisticalDailyFoodRankingInformation() {
        System.out.println(DateUtil.now() + "执行每日0时统计食物信息任务开始===========================");
        for (UserFoodCount countUserTag : foodService.countUserFoods()) {
            UserMeal userMeal = userMealService.getById(countUserTag.getUserId());
            rankingService.save(new Ranking().setDate(DateUtil.now())
                    .setNum(countUserTag.getCount()).setUserId(countUserTag.getUserId())
                    .setType("食物").setUserName(userMeal.getName())
                    .setUserAvatar(userMeal.getAvatar()));
        }
        System.out.println(DateUtil.now() + "执行每日0时统计食物信息任务结束===========================");
    }

    /**
     * 每小时统计一次选餐信息排行榜，更新当天的num，延迟半分钟执行
     */
    @Scheduled(cron = "30 2 */1 * * ?")
    public void statisticalHoursPickRankingInformation() {
        System.out.println(DateUtil.now() + "执行每小时统计选餐信息任务开始==========================");
        for (UserPickCount countUsePick : pickLogService.countUserPicks()) {
            rankingService.update(new UpdateWrapper<Ranking>()
                    .set("num", countUsePick.getCount())
                    .likeRight("date", DateUtil.now().substring(0, 10))
                    .eq("user_id", countUsePick.getUserId())
                    .eq("type", "选餐"));
        }
        System.out.println(DateUtil.now() + "执行每小时统计选餐信息任务结束==========================");
    }

    /**
     * 每天00:00:02统计一次选餐信息排行榜，新增一条记录
     */
    @Scheduled(cron = "0 2 0 * * ?")
    public void statisticalDailyPickRankingInformation() {
        System.out.println(DateUtil.now() + "执行每日0时统计选餐信息任务开始==========================");
        for (UserPickCount countUsePick : pickLogService.countUserPicks()) {
            UserMeal userMeal = userMealService.getById(countUsePick.getUserId());
            rankingService.save(new Ranking().setDate(DateUtil.now())
                    .setNum(countUsePick.getCount()).setUserId(countUsePick.getUserId())
                    .setType("选餐")
                    .setUserName(userMeal.getName())
                    .setUserAvatar(userMeal.getAvatar()));
        }
        System.out.println(DateUtil.now() + "执行每日0时统计选餐信息任务结束==========================");
    }

}
